package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.organization.util.SaleBranch;
import com.turing.post.organization.util.SaleBranchBusiness;
import com.turing.post.organization.util.SaleBranchDao;
import com.turing.post.organization.util.SysBranch;
import com.turing.post.organization.util.SysBranchDao;
import com.turing.post.organization.util.SysBranchSubord;
import com.turing.post.organization.util.SysBranchSubordDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Dec 18, 2012
 */
@Stateless(mappedName = "organization.impl.SaleBranchBusinessImpl")
@Remote
@Local
public class SaleBranchBusinessImpl implements SaleBranchBusiness {

	@EJB(mappedName = "organization.impl.SaleBranchDaoImpl")
	private SaleBranchDao saleBranchDaoImpl;

	@EJB(mappedName = "organization.impl.SysBranchDaoImpl")
	private SysBranchDao sysBranchDaoImpl;

	@EJB(mappedName = "organization.impl.SysBranchSubordDaoImpl")
	private SysBranchSubordDao sysBranchSubordDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	@Override
	public void insertSaleBranchInfo(PCPSWebInput webInput,
			SaleBranch saleBranch) throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 业务检查
		if (saleBranch == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
					"SaleBranch");
		}
		// 编号
		if (PCPSUtil.isEmpty(saleBranch.getSaleBranchNo())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
					"SaleBranchNo");
		}
		// 名称
		if (PCPSUtil.isEmpty(saleBranch.getSaleBranchName())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
					"SaleBranchName");
		}
		// 验证机构号是否已经使用
		if (saleBranchDaoImpl.getSaleBranchBySaleBranchNo(saleBranch
				.getSaleBranchNo()) != null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.BRANCHNO_EXIST);
		}
		// 备付金账户不能为空
		if (saleBranch.getBankAccountId() == null) {
			throw new OrganApplicationException("备付金账户不能为空!");
		}
		// 平台机构管理员归属机构号为空
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			// int length = webInput.getOperBranchNo().length();
			// 验证登录用户是否归属为当前发卡商
			if (!PCPSUtil.getIssueCompanyNo(webInput).equals(
					saleBranch.getSaleBranchNo().substring(0, 6))) {
				throw new OrganApplicationException(
						OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
						OrganRespCode.ISSUE_INSERT_UPDATE,
						"无权添加其他销售商分支机构以及下属分支机构!");
			}
		}
		// 收益归属ID
		if (saleBranch.getIncomeBranchId() == null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "收益归属机构编号为空!");
		}
		// 默认赋值
		saleBranch.setCreditLowLimit(BigInteger.ZERO);
		saleBranch.setLastCredit(BigInteger.ZERO);
		saleBranch.setCredit(BigInteger.ZERO);
		saleBranch.setLastCash(BigInteger.ZERO);
		saleBranch.setCash(BigInteger.ZERO);
		saleBranch.setAlreadyCheckCash(BigInteger.ZERO);
		saleBranch.setLastCashFee(BigInteger.ZERO);
		// saleBranch.setLastCashFeeDate(lastCashFeeDate);创建时为空
		saleBranch.setDayOff(SaleBranch.Not_DayOff);
		saleBranch.setStatus(SaleBranch.SALEBRANCH_STATUS_ACTIVATED);// 创建默认激活
		// 插入平台机构表
		SysBranch sysBranch = new SysBranch(saleBranch.getSaleBranchNo(),
				SysBranch.SYSBRANCH_SALEBRANCH);
		sysBranchDaoImpl.insertSysBranch(sysBranch);
		// 结构树数据
		String issueBranchNo = saleBranch.getIssueBranchNo();
		String saleBranchNo = saleBranch.getSaleBranchNo();
		String superSaleBranchNo = saleBranch.getSuperSaleBranchNo();
		// 插入销售机构表
		saleBranchDaoImpl.insertSaleBranch(saleBranch);
		// 销售商分支机构============
		SysBranchSubord sysBranchSub = null;
		if (!PCPSUtil.isEmpty(issueBranchNo)
				&& PCPSUtil.isEmpty(superSaleBranchNo)) {// 直接隶属于发卡机构
			// 平台机构隶属关系
			List<SysBranchSubord> sysBranchSubordList = sysBranchSubordDaoImpl
					.getSysBranchSubordListBySubBranchNo(issueBranchNo);
			// 插入所有父节点对应关系
			SysBranchSubord sbs = null;
			SysBranchSubord _sbs = null;
			for (int i = 0; i < sysBranchSubordList.size(); i++) {
				_sbs = sysBranchSubordList.get(i);
				sbs = new SysBranchSubord();
				sbs.setBranchNo(_sbs.getBranchNo());// 机构号
				sbs.setSubBranchNo(saleBranchNo);// 添加机构机构号
				sbs.setSubBranchType(SysBranch.SYSBRANCH_SALEBRANCH);// 子机构类型
				sbs.setSubBranchSuperNo(issueBranchNo);// 上级机构号
				sysBranchSubordDaoImpl.insertSysBranchSubord(sbs);
			}
			// 自己添加自己
			sysBranchSub = new SysBranchSubord();
			sysBranchSub.setBranchNo(saleBranchNo);
			sysBranchSub.setSubBranchNo(saleBranchNo);
			sysBranchSub.setSubBranchType(SysBranch.SYSBRANCH_SALEBRANCH);
			sysBranchSubordDaoImpl.insertSysBranchSubord(sysBranchSub);
		}
		if (!PCPSUtil.isEmpty(superSaleBranchNo)) {// 归属于销售商分支机构
			// 平台机构隶属关系
			List<SysBranchSubord> sysBranchSubordList = sysBranchSubordDaoImpl
					.getSysBranchSubordListBySubBranchNo(superSaleBranchNo);
			// 插入所有父节点对应关系
			SysBranchSubord sbs = null;
			SysBranchSubord _sbs = null;
			for (int i = 0; i < sysBranchSubordList.size(); i++) {
				_sbs = sysBranchSubordList.get(i);
				sbs = new SysBranchSubord();
				sbs.setBranchNo(_sbs.getBranchNo());
				sbs.setSubBranchNo(saleBranchNo);// 添加机构机构号
				sbs.setSubBranchType(SysBranch.SYSBRANCH_SALEBRANCH);
				sbs.setSubBranchSuperNo(superSaleBranchNo);
				sysBranchSubordDaoImpl.insertSysBranchSubord(sbs);
			}
			// 自己添加自己
			sysBranchSub = new SysBranchSubord();
			sysBranchSub.setBranchNo(saleBranchNo);
			sysBranchSub.setSubBranchNo(saleBranchNo);
			sysBranchSub.setSubBranchType(SysBranch.SYSBRANCH_SALEBRANCH);
			sysBranchSubordDaoImpl.insertSysBranchSubord(sysBranchSub);
		}
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "创建销售商机构", saleBranch,
				SysOperLog.operType_Add, "SaleBranchName:"
						+ saleBranch.getSaleBranchName());
	}

	@Override
	public void updateSaleBranchCreditLowLimit(PCPSWebInput webInput,
			SaleBranch saleBranch) throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		SaleBranch _saleBranch = saleBranchDaoImpl
				.getSaleBranchForUpdate(saleBranch.getSaleBranchNo());
		// 业务检查
		if (_saleBranch == null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "待修改销售商分支机构数据库不存在!");
		}
		// 平台机构管理员归属机构号为空
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			// int length = webInput.getOperBranchNo().length();
			// 验证登录用户是否归属为当前发卡商
			if (!PCPSUtil.getIssueCompanyNo(webInput).equals(
					_saleBranch.getSaleBranchNo().substring(0, 6))) {
				throw new OrganApplicationException(
						OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
						OrganRespCode.ISSUE_INSERT_UPDATE,
						"无权修改其他销售商分支机构以及下属分支机构!");
			}
		}
		_saleBranch.setCreditLowLimit(saleBranch.getCreditLowLimit());
		saleBranchDaoImpl.updateSaleBranch(_saleBranch);
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "修改销售商机构预存款下限", saleBranch,
				SysOperLog.operType_Edit, "SaleBranchName:"
						+ saleBranch.getSaleBranchName());
	}

	@Override
	public void updateSaleBranchInfo(PCPSWebInput webInput,
			SaleBranch saleBranch) throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		SaleBranch _saleBranch = saleBranchDaoImpl
				.getSaleBranchForUpdate(saleBranch.getSaleBranchNo());
		// 业务检查
		if (_saleBranch == null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "待修改销售商分支机构数据库不存在!");
		}
		// 平台机构管理员归属机构号为空
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			// int length = webInput.getOperBranchNo().length();
			// 验证登录用户是否归属为当前发卡商
			if (!PCPSUtil.getIssueCompanyNo(webInput).equals(
					_saleBranch.getSaleBranchNo().substring(0, 6))) {
				throw new OrganApplicationException(
						OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
						OrganRespCode.ISSUE_INSERT_UPDATE,
						"无权修改其他销售商分支机构以及下属分支机构!");
			}
		}
		// 对象赋值
		_saleBranch.setSaleBranchName(saleBranch.getSaleBranchName());
		_saleBranch.setStatus(saleBranch.getStatus());// 修改激活状态
		_saleBranch.setIncomeBranchId(saleBranch.getIncomeBranchId());
		_saleBranch.setAddress(saleBranch.getAddress());
		_saleBranch.setLinkTele(saleBranch.getLinkTele());
		_saleBranch.setLinkMan(saleBranch.getLinkMan());
		// 默认备付金账户
		if (saleBranch.getBankAccountId() != null
				&& saleBranch.getBankAccountId().compareTo(BigInteger.ZERO) > 0) {
			_saleBranch.setBankAccountId(saleBranch.getBankAccountId());
		}
		saleBranchDaoImpl.updateSaleBranch(_saleBranch);
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "修改销售商机构", saleBranch,
				SysOperLog.operType_Edit, "SaleBranchName:"
						+ saleBranch.getSaleBranchName());
	}

	@Override
	public void deleteSaleBranchInfo(PCPSWebInput webInput, String branchNo)
			throws PCPSApplicationException {
		saleBranchDaoImpl.deleteSaleBranch(branchNo);
		sysBranchSubordDaoImpl.deleteSysBranchSubord(branchNo);
		sysBranchDaoImpl.deleteSysBranch(branchNo);
	}

	private void insertRecordingOperationLog(PCPSWebInput webInput,
			String logName, SaleBranch obj, String operType,
			String lastFieldName) throws PCPSApplicationException {
		String logRemark = PCPSUtil.RecordingOperationLog(obj, lastFieldName);
		if (!PCPSUtil.isEmpty(logRemark)) {
			// 插入数据库日志
			SysOperLog sysOperLog = new SysOperLog();
			if (webInput != null && webInput.getOperId() != null) {
				sysOperLog.setOperId(webInput.getOperId());
				sysOperLog.setOperType(operType);
				sysOperLog.setLogName(logName);
				if (webInput != null) {
					sysOperLog.setIpAddress(webInput.getIpAddress());
				}
				if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
					sysOperLog.setIssueCompanyNo(webInput.getOperBranchNo()
							.substring(0, 6));
					sysOperLog.setBranchNo(webInput.getOperBranchNo());
				}
				sysOperLog.setRemark(logRemark);
				sysOperLogDaoImpl.insertSysOperLog(sysOperLog);
			}
		}
	}
}
